package service

import (
	"net/http"
	"sync"

	"github.com/gorilla/websocket"
)

var upgrader = websocket.Upgrader{
	CheckOrigin: func(r *http.Request) bool { return true },
}

type WsServer struct {
	connections map[string]*websocket.Conn // userID -> conn
	mu          sync.RWMutex
}

func NewWsServer() *WsServer {
	return &WsServer{
		connections: make(map[string]*websocket.Conn),
	}
}

func (s *WsServer) HandleWS(w http.ResponseWriter, r *http.Request) {
	userID := r.URL.Query().Get("user_id")
	if userID == "" {
		http.Error(w, "user_id required", http.StatusBadRequest)
		return
	}
	conn, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		return
	}
	s.mu.Lock()
	s.connections[userID] = conn
	s.mu.Unlock()
	// 监听消息、处理断开等
}
